﻿<script type="text/javascript">

    $(document).ready(function () {
        getButton("dead");  //请求当前用户可以操作的按钮                        
    });

    function ui_dead_init_list(toolbar) {
        $("#ui_dead_dg").datagrid({       //初始化datagrid
            url: "ashx/bg_dead.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: false,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'name', title: '姓名', width: 100 }
            ]],
            queryParams: {
                flag: 1
            },
            columns: [[
                {
                    field: 'code', title: '编码', width: 150,
                    formatter: function (value, row, index) {
                        return value ? '<a target="_blank" href="/admin/ashx/queryqrcode.aspx?ids=' + row.id + '">' + value + '</a>' : value;
                    }
                },
                { field: 'gender', title: '性别', width: 40 },


                { field: 'actFiretime', title: '火化时间', sortable: true, width: 150 },
                { field: 'estStoretime', title: '预存日期', sortable: true, width: 150 },
                {field: 'estStoreplace', title: '预存骨灰堂', sortable: true, width: 160},
                { field: 'areaName', title: '村（镇）', width: 100 },
                { field: 'relativeName', title: '联系人姓名', width: 100 },
                { field: 'relativeTel', title: '联系人电话', width: 100 },
                { field: 'relationId', title: '与死者关系', width: 100 },
                { field: 'birthAddress', title: '详细地址', width: 250},
                { field: 'birthday', title: '出生日期', width: 150 },
                { field: 'deathtime', title: '死亡时间', width: 150 } ,
                { field: 'cardno', title: '死者证件号', width: 150 },

                {
                    field: 'graveName', title: '骨灰存放位置', width: 200,
                    formatter: function (value, row, index) {
                        return value && value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                    }
                }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,   //请求回来的当前用户可以操作的按钮
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_dead_edit();
            }
        });
    }

    function ui_dead_add() {
        $("<div/>").dialog({
            id: "ui_dead_add_dialog",
            href: "html/ui_dead_edit.html",
            iconCls: "icon-add",
            title: "死者登记",
            height: 480,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_dead_add_btn",
                text: '添加',
                iconCls: "icon-add",
                handler: function () {
                    $("#ui_dead_editform").form("submit", {
                        url: "ashx/bg_dead.ashx",
                        onSubmit: function (param) {

                            $('#ui_dead_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_dead_add_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_dead_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_dead_add_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_dead_name").focus();
            },
            onClose: function () {
                $("#ui_dead_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_dead_edit() {
        var rows = $("#ui_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的行或者双击某行");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];

        $("<div/>").dialog({
            id: "ui_dead_edit_dialog",
            href: "html/ui_dead_edit.html",
            iconCls: "icon-edit",
            title: "修改死者信息",
            height: 480,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_dead_edit_btn",
                text: '修 改',
                iconCls: "icon-edit",
                handler: function () {
                    $("#ui_dead_editform").form("submit", {
                        url: "ashx/bg_dead.ashx",
                        onSubmit: function (param) {
                            $('#ui_dead_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_dead_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_dead_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_dead_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_dead_id").val(row.id);
                $("#ui_dead_relative_id").val(row.relativeId);
                $("#ui_dead_name").textbox('setValue', row.name);
                $("#ui_dead_cardno").textbox('setValue', row.cardno);
                $("#ui_dead_gender").combobox('setValue', row.gender);
                $('#ui_dead_nation').combobox('setValue', row.nationId);
                $('#ui_dead_profession').textbox('setValue', row.profession);
                $('#ui_dead_education').textbox('setValue', row.education);
                $('#ui_dead_deathcause').textbox('setValue', row.deathcause);
                $('#ui_dead_deathplace').textbox('setValue', row.deathplace);
                $("#ui_dead_birthday").datebox('setValue', row.birthday);
                $("#ui_dead_deathtime").datetimebox('setValue', row.deathtime);
                $("#ui_dead_areaId").combotree('setValue', row.areacode);
                $("#ui_dead_photo").val(row.photo);
                $("#ui_dead_photo_img").attr('src', row.photo + "?t=" + new Date().getTime());
                $("#ui_dead_birAddress").textbox('setValue', row.birAddress);
                $("#ui_dead_homeAddress").textbox('setValue', row.homeAddress);
                $("#ui_dead_estArrivetime").datetimebox('setValue', row.estArrivetime);
                $("#ui_dead_estArriveplace").textbox('setValue', row.estArriveplace);
            },
            onClose: function () {
                $("#ui_dead_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    /*
    预订殡仪馆
    */
    function ui_dead_reserve() {
        var rows = $("#ui_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请选择一个死者");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量操作");
            $("#ui_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];

        $("<div/>").dialog({
            id: "ui_dead_reserve_dialog",
            href: "html/ui_dead_reserve.html",
            iconCls: "icon-reserve",
            title: "预订殡仪馆",
            height: 280,
            width: 400,
            modal: true,
            buttons: [{
                id: "ui_dead_reserve_btn",
                text: '预订',
                iconCls: "icon-reserve",
                handler: function () {
                    $("#ui_dead_reserve_form").form("submit", {
                        url: "ashx/bg_dead.ashx",
                        onSubmit: function (param) {
                            $('#ui_dead_reserve_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'reserve';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_dead_reserve_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_dead_reserve_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_dead_reserve_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {

                $("#ui_dead_id").val(row.id);
                $("#ui_relative_id").val(row.relativeId);

                $("#ui_dead_name").textbox('setValue', row.name);
                $("#ui_relative_name").textbox('setValue', row.relativeName);
                $("#ui_relative_tel").textbox('setValue', row.relativeTel);
                $("#ui_relative_relation").textbox('setValue', row.relationId);
                $("#ui_dead_estArrivetime").datetimebox('setValue', row.estArrivetime);
                $("#ui_dead_estArriveplace").combobox('setValue', row.estArriveplace);
            },
            onClose: function () {
                $("#ui_dead_reserve_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    /*
   预约存灰堂
   */
    function ui_dead_reserve2() {
        var rows = $("#ui_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请选择一个死者");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量操作");
            $("#ui_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];

        $("<div/>").dialog({
            id: "ui_dead_reserve2_dialog",
            href: "html/ui_dead_reserve2.html",
            iconCls: "icon-reserve2",
            title: "预约骨灰堂",
            height: 320,
            width: 400,
            modal: true,
            buttons: [{
                id: "ui_dead_reserve2_btn",
                text: '预约',
                iconCls: "icon-reserve2",
                handler: function () {
                    $("#ui_dead_reserve2_form").form("submit", {
                        url: "ashx/bg_dead.ashx",
                        onSubmit: function (param) {
                            $('#ui_dead_reserve2_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'reserve2';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_dead_reserve2_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_dead_reserve2_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_dead_reserve2_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {

                $("#ui_dead_id").val(row.id);
                $("#ui_relative_id").val(row.relativeId);
                $("#ui_dead_name").textbox('setValue', row.name);
                $("#ui_dead_cardno").textbox('setValue', row.cardno);
                $("#ui_relative_name").textbox('setValue', row.relativeName);
                $("#ui_relative_tel").textbox('setValue', row.relativeTel);
                $("#ui_relative_relation").textbox('setValue', row.relationId);
                $("#ui_dead_firetime").datetimebox('setValue', row.actFiretime);
                $("#ui_dead_estStoretime").datetimebox('setValue', row.estStoretime);
                $("#ui_dead_estStoreplace").combotree('setValue', row.estStoreplace);
            },
            onClose: function () {
                $("#ui_dead_reserve2_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_dead_delete() {
        var rows = $("#ui_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的行");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的' + rows.length + '行？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                para.id = "";
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.id = ids.join(",");
                $.ajax({
                    url: "ashx/bg_dead.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", data.msg);
                            $("#ui_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }

    function ui_dead_searchdata() {
        var whereStr = "1=1";
        var code = $('#ui_dead_search').find('[name=ui_dead_search_code]').val();
        var name = $('#ui_dead_search').find('[name=ui_dead_search_username]').val();
        var cardno = $('#ui_dead_search').find('[name=ui_dead_search_cardno]').val();
        var areaid = $('#ui_dead_search_area').combotree('getValue');
        var starttime = $('#ui_dead_search').find('[name=ui_dead_search_dead_datestart]').val();
        var endtime = $('#ui_dead_search').find('[name=ui_dead_search_dead_dateend]').val();
        var bStore = $('#ui_dead_search').find('[name=ui_dead_search_store]').val();
        if (code != "") {
            whereStr += " and code LIKE '%" + code + "%'";
        }
        if (name != "") {
            whereStr += " and name LIKE '%" + name + "%'";
        }
        if (cardno != "") {
            whereStr += " and cardno='" + cardno + "'";
        }
        if (areaid != "") {
            var aid = areaid.substr(9, 3);
            if (aid == "000") {
                whereStr += " and left(areacode,9)='" + areaid.substr(0, 9) + "'";
            } else {
                whereStr += " and areacode='" + areaid + "'";
            }
        }
        if (starttime != "") {
            whereStr += " and starttime > '" + starttime + "'";
        }
        if (endtime != "") {
            whereStr += " and starttime < '" + endtime + "'";
        }
        if (bStore == "Y") {
            whereStr += " and not starttime is null";
        } else if (bStore == "N") {
            whereStr += " and starttime is null";
        }

        $("#ui_dead_dg").datagrid('load', { where: whereStr,flag:1 });
        $("#ui_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_operatelog_layout').layout('collapse', 'east');    //隐藏
    }
    function ui_dead_cleardata() {
        $('#ui_dead_search input').val('');
        $('#ui_dead_search select').val('select');
        $('#ui_dead_search_dead_datestart').datebox('setValue', '');
        $('#ui_dead_search_dead_dateend').datebox('setValue', '');
        $("#ui_dead_dg").datagrid('load', { flag: 1 });

        $("#ui_dead_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_operatelog_layout').layout('collapse', 'east');    //隐藏
    }
    function ui_dead_qrcode() {
        var rows = $("#ui_dead_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要生成二维码的行");
            return;
        }
        var ids = [];
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }
        var url = "/admin/ashx/queryqrcode.aspx?ids=" + ids.join(',');
        window.open(url);
    }

    //上传导入数据
    function ui_dead_import() {

        $("<div/>").dialog({
            id: "ui_dead_import_dialog",
            href: "html/ui_dead_import.html",
            iconCls: "icon-filter",
            title: '数据导入',
            height: 240,
            width: 420,
            modal: true,
            buttons: [{
                id: "ui_dead_import_btn",
                text: '导入',
                iconCls: "icon-import",
                handler: function () {
                    $("#ui_dead_importform").form("submit", {
                        url: "ashx/bg_dead.ashx",
                        onSubmit: function (param) {
                            $('#ui_dead_import_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            var chkAuto = $('#chkAutoCreateGrave').val();
                            param.action = 'import';
                            param.op = 'import';
                            param.autocreate = chkAuto;
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_dead_import_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式                                    
                            if (dataJson.success) {
                                $("#ui_dead_import_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_dead_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_dead_import_btn').linkbutton('enable');  //恢复按钮
                                //$('#ui_dead_importform_msg').html("导入出错，请查看<a href='" + dataJson.msg + "' target='_blank'>错误日志</a>");
                                //$.show_warning("提示",  "导入失败！");
                                $('#ui_dead_importform_msg').html("导入出错:" + dataJson.msg);
                                $.show_alert("导入失败", dataJson.msg);

                            }
                        }
                    });
                }
            }],
            onLoad: function () {
            },
            onClose: function () {
                $("#ui_dead_import_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //处理输入编码后回车搜索
    function ui_dead_keyEnter(e) {
        if (e.keyCode == 13) {
            var code = $('#ui_dead_search_code').textbox("getValue");
            var x = code.indexOf("=");
            if (x > 0) {
                code = code.substr(x + 1);
                $('#ui_dead_search_code').textbox("setValue",code);
            }
            if (code && code.length > 0) {
                ui_dead_searchdata();
            }
        }
    }

</script>
<div id="ui_dead_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <table id="ui_dead_dg" data-options="fit:true,border:false">
        </table>
    </div>
    <div data-options="region:'west',split:true,border:true,collapsed:true" title="多条件查询"
        style="width: 280px;">
        <div id="ui_dead_search">
            <table class="tableForm" style="width: 100%; height: 100%; background: #F5F5F5;">
                <tr>
                    <th>死者编码：
                    </th>
                    <td>
                        <input class="easyui-textbox" name="ui_dead_search_code" data-options="events:{keyup:ui_dead_keyEnter}" id="ui_dead_search_code" autocomplete="off" style="width: 130px;" />
                    </td>
                </tr>
                <tr>
                    <th>死者姓名：
                    </th>
                    <td>
                        <input class="easyui-textbox" name="ui_dead_search_username" id="ui_dead_search_username" autocomplete="off" style="width: 130px;" />
                    </td>
                </tr>
                <tr>
                    <th>身份证号：
                    </th>
                    <td>
                        <input class="easyui-textbox" name="ui_dead_search_cardno" id="ui_dead_search_cardno" autocomplete="off" style="width: 130px;" />
                    </td>
                </tr>
                <tr>
                    <th>是否入葬：
                    </th>
                    <td>
                        <select class="easyui-combobox" name="ui_dead_search_store" id="ui_dead_search_store" data-options="panelHeight:65,editable:false">
                            <option value="A">请选择</option>
                            <option value="Y">是</option>
                            <option value="N">否</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>所属地区：
                    </th>
                    <td>
                        <input class="easyui-combotree" id="ui_dead_search_area" name="ui_dead_search_area"
                            data-options="url:'ashx/bg_area.ashx?action=getall', lines:true" />
                    </td>
                </tr>
                <tr>
                    <th>死亡日期：
                    </th>
                    <td>
                        <input name="ui_dead_search_dead_datestart" id="ui_dead_search_dead_datestart" class="easyui-datebox"
                            editable="false" style="width: 140px;" />
                    </td>
                </tr>
                <tr>
                    <th>至：
                    </th>
                    <td>
                        <input name="ui_dead_search_dead_dateend" id="ui_dead_search_dead_dateend" class="easyui-datebox"
                            editable="false" style="width: 140px;" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                            onclick="ui_dead_searchdata();">搜索</a>
                    </td>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                            onclick="ui_dead_cleardata();">清空条件</a>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>
